package org.yscz.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.yscz.entity.Scale;
import org.yscz.entity.ScaleProject;
import org.yscz.entity.ScaleType;
import org.yscz.entity.vo.ScaleTypeVo;
import org.yscz.mapper.ScaleMapper;
import org.yscz.mapper.ScaleProjectMapper;
import org.yscz.mapper.ScaleTypeMapper;
import org.yscz.service.ProjectService;
import org.yscz.service.ScaleTypeService;

import java.util.*;
import java.util.stream.Collectors;

/**
 * @author muqs
 * @create 2021-08-18 17:48
 */
@Service
@Slf4j
public class ScaleTypeServiceImpl implements ScaleTypeService {

    @Autowired
    private ScaleTypeMapper scaleTypeMapper;

    @Autowired
    private ScaleMapper scaleMapper;

    @Autowired
    private ScaleProjectMapper scaleProjectMapper;

    @Override
    public ScaleType add(ScaleType scaleType) {
        scaleType.setUpdateTime(new Date());
        scaleTypeMapper.insert(scaleType);
        return scaleType;
    }

    @Override
    public void modify(ScaleType scaleType) {
        scaleType.setUpdateTime(new Date());
        scaleTypeMapper.updateById(scaleType);
    }

    @Override
    public void del(Long scaleTypeId) {

        ScaleType scaleType = scaleTypeMapper.selectById(scaleTypeId);
        if (scaleType != null) {
            if (!"默认".equals(scaleType.getScaleTypeName())) {
                scaleTypeMapper.deleteById(scaleTypeId);

                // 将改量表分类下的量表数据改为"默认"分类
                QueryWrapper queryWrapper = new QueryWrapper();
                queryWrapper.eq("scale_type_name", "默认");
                ScaleType defaultScaleType = scaleTypeMapper.selectOne(queryWrapper);

                Scale scale = new Scale();
                scale.setScaleTypeIdFk(defaultScaleType.getScaleTypeId());
                UpdateWrapper updateWrapper = new UpdateWrapper();
                updateWrapper.eq("scale_type_id_fk", scaleTypeId);
                scaleMapper.update(scale, updateWrapper);
            } else {
                log.info("【默认】量表分类不允许删除");
                return;
            }
        }
    }

    @Override
    public IPage<ScaleType> listPage(QueryWrapper queryWrapper, IPage<ScaleType> page) {
        return scaleTypeMapper.selectPage(page, queryWrapper);
    }

    @Override
    public List<ScaleType> list(QueryWrapper queryWrapper) {
        return scaleTypeMapper.selectList(queryWrapper);
    }

//    @Override
//    public IPage<ScaleType> listPageByCondition(IPage<ScaleTypeVo> page, Map condition) {
//        return scaleTypeMapper.listPageByCondition(page, condition);
//    }

    @Override
    public IPage<ScaleType> listPageByCondition(IPage<ScaleTypeVo> page, Map condition) {
        return scaleTypeMapper.listPageByCondition(page, condition);
    }

    @Override
    public List<ScaleTypeVo> listPageByConditionByProject(Long projectId, Integer pageNum, Integer pageSize, Map condition) {
        Integer startPageNum = (pageNum - 1) * pageSize;
        return scaleTypeMapper.listPageByConditionAndProject(projectId, startPageNum, pageSize, condition);
    }

    @Override
    public Integer selectCountByProjectId(Long projectId) {
        return scaleTypeMapper.selectCountByProjectId(projectId);
    }

    /*@Override
    public List<Map<String, Object>> listPageByCondition(Long projectId, Integer pageNum, Integer pageSize) {
        //关联表查询出行业对应的量表
        QueryWrapper<ScaleProject> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("project_id", projectId);
        List<ScaleProject> scaleProjects = scaleProjectMapper.selectList(queryWrapper);

        //根据查询出的量表去重量表id
        Iterator<ScaleProject> scaleProjectIterator = scaleProjects.iterator();
        HashSet<Long> scaleSet = new HashSet<>();
        while (scaleProjectIterator.hasNext()) {
            ScaleProject scaleProject = scaleProjectIterator.next();
            scaleSet.add(scaleProject.getScaleId());
        }

        //根据量表id查询出量表分类id
        Iterator<Long> scaleTypeIterator = scaleSet.iterator();
        HashSet<Long> scaleTypeSet = new HashSet<>();
        while (scaleTypeIterator.hasNext()) {
            Long scaleId = scaleTypeIterator.next();
            Scale scale = scaleMapper.selectById(scaleId);
            scaleTypeSet.add(scale.getScaleTypeIdFk());
        }

        //根据量表分类id查询出量表分类
        List<Map<String, Object>> scaleTypeList = new ArrayList<>();
        Map<String, Object> map = null;
        for (Long scaleTypeId : scaleTypeSet) {
            map = new HashMap<>();
            ScaleType scaleType = scaleTypeMapper.selectById(scaleTypeId);
            //根据量表分类id查询出对应的量表
            //QueryWrapper<Scale> scaleQueryWrapper = new QueryWrapper<>();
            //scaleQueryWrapper.eq("scale_type_id_fk", scaleTypeId);
            //List<Scale> scales = scaleMapper.selectList(scaleQueryWrapper);
            //根据量表分类id查询出对应量表下数量
            QueryWrapper<Scale> scaleCountQueryWrapper = new QueryWrapper<>();
            scaleCountQueryWrapper.eq("scale_type_id_fk", scaleType.getScaleTypeId());
            scaleCountQueryWrapper.groupBy("scale_type_id_fk");
            Integer scaleCount = scaleMapper.selectCount(scaleCountQueryWrapper);

            //根据scaleTypeId组织数据
            map.put("scaleType", scaleType);
            //map.put("scale", scales);
            map.put("scaleCount", scaleCount);

            scaleTypeList.add(map);
        }
        log.info("scaleTypeList:[{}]", scaleTypeList);

        //分页并根据排序号升序排序
        List<Map<String, Object>> pageScaleTypeList = scaleTypeList.stream().skip((pageNum - 1) * pageSize).limit(pageSize).sorted((map1, map2) ->
                ((ScaleType) (map1.get("scaleType"))).getScaleTypeSort().compareTo(((ScaleType) (map2.get("scaleType"))).getScaleTypeSort())
        ).collect(Collectors.toList());

        //根据name模糊查询


        return pageScaleTypeList;
    }*/
}
